<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" width="40%" @closed="dialogClosed()">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-form-item label="组名" prop="name">
        <el-input v-model="dataForm.name" placeholder="组名"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model.number="dataForm.sort" placeholder="排序"></el-input>
      </el-form-item>
      <el-form-item label="组图标" prop="icon">
        <el-input v-model="dataForm.icon" placeholder="组图标"></el-input>
      </el-form-item>
      <el-form-item label="所属分类" prop="categoryId">
        <!--
          v-model：绑定数据
          options：数组格式
          props：显示字段及默认值
          filterable：可搜索功能
          链接地址：https://element.eleme.cn/#/zh-CN/component/cascader
        -->
        <el-cascader v-model="dataForm.categoryIds" :options="categorys" :props="props"  placeholder="试试搜索：手机"  filterable></el-cascader>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      categorys: [],
      props: {
        value: "id",
        label: "name",
        children: "children",
      },
      visible: false,
      dataForm: {
        id: 0,
        name: "",
        sort: "",
        icon: "",
        categoryIds: [],
        categoryId: 0,
        remark: "",
      },
      dataRule: {
        name: [{ required: true, message: "组名不能为空", trigger: "blur" }],
        sort: [{
            validator: (rule, value, callback) => {
              console.log("输入的值为:",value);
              if (value === "") {
                callback(new Error("排序字段必须填写"));
              } else if (!Number.isInteger(value) || value < 0) {
                callback(new Error("排序字段为大于0的整数"));
              } else {
                callback();
              }
            },
            trigger: "blur",
            required: true
          }],
        icon: [{ required: true, message: "组图标不能为空", trigger: "blur" }],
        categoryId: [
          { required: true, message: "所属分类不能为空", trigger: "blur" },
        ],
        remark: [{ required: true, message: "备注不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getCategorys();
  },
  methods: {
    //对话框关闭清空内容
    dialogClosed(){
      this.dataForm.categoryIds = []
    },
    //获取数据列表
    getCategorys() {
      this.$http({
        url: this.$http.adornUrl("/product/category/list/tree"),
        method: "get",
      }).then(({ data }) => {
        console.log("获取菜单数据", data.data);
        this.categorys = data.data;
      });
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/product/attrgroup/info/${this.dataForm.id}`),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.attrGroup.name;
              this.dataForm.sort = data.attrGroup.sort;
              this.dataForm.icon = data.attrGroup.icon;
              this.dataForm.categoryIds = data.attrGroup.categoryIds;
              this.dataForm.remark = data.attrGroup.remark;
            }
            console.log("修改回写的数据为:", this.dataForm);
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/product/attrgroup/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              name: this.dataForm.name,
              sort: this.dataForm.sort,
              icon: this.dataForm.icon,
              categoryId: this.dataForm.categoryIds[this.dataForm.categoryIds.length - 1],
              remark: this.dataForm.remark
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
